/**
 * @version: V1.0
 * @Title: Test08_FindFirstIntersectNode.java
 * @Description:
 * @author wangshipeng
 * @date 2022-01-18 23:03
 * @Copyright: 2022-01-18
 */

package com.peng.linkedlist;

/**
 * @author wangshipeng
 * @ClassName: Test08_FindFirstIntersectNode
 * @Description: 两个链表， 求第一个相交点
 * @date 2022-01-18 23:03
 */
public class Test08_FindFirstIntersectNode {


    public static Node findFirstIntersectNode(Node head1, Node head2) {
        if (head1 == null || head2 == null) {
            return null;
        }

        Node loop1 = getLoopNode(head1);
        Node loop2 = getLoopNode(head2);

        //两个都不是有环链表
        if (loop1 == null && loop2 == null) {
            return noLoop(head1, head2);
        }
        //两个都是有环链表
        if (loop1 != null && loop2 != null) {
            return loop(head1, head2, loop1, loop2);
        }
        //如果一个有环，有个无环
        return null;
    }

    private static Node loop(Node head1, Node head2, Node loop1, Node loop2) {
        Node res;
        //如果两个链表的第一个入环点是一样的
        //处理其实和两个无环链表寻找交点是一样的
        if (loop1 == loop2) {
            int n = 0;
            Node cur1 = head1;
            Node cur2 = head2;
            while (cur1 != loop1) {
                n++;
                cur1 = cur1.next;
            }
            while (cur2 != loop2) {
                n--;
                cur2 = cur2.next;
            }

            cur1 = n > 0 ? head1 : head2;//较长的链表
            cur2 = cur1 == head1 ? head2 : head1;//较短的链表
            n = Math.abs(n);

            //先让较长的链表走n步，让两个链表持平
            while (n != 0) {
                cur1 = cur1.next;
                n--;
            }
            //两个链表同步向下:
            // 如果中途能碰上，则获得第一个交点，否则说明两链表无交点
            while (cur1 != cur2) {
                cur1 = cur1.next;
                cur2 = cur2.next;
            }
            res = cur1;
        } else {
            Node cur = loop1.next;
            while (cur != loop2) {
                if (cur == loop1) {
                    return null;
                }
                cur = cur.next;
            }

            res = cur;


        }

        return res;
    }

    /**
     * 两个无环节点的处理
     *
     * @param head1
     * @param head2
     * @return
     */
    private static Node noLoop(Node head1, Node head2) {
        int n = 0;
        Node cur1 = head1;
        Node cur2 = head2;
        while (cur1 != null) {
            n++;
            cur1 = cur1.next;
        }
        while (cur2 != null) {
            n--;
            cur2 = cur2.next;
        }

        cur1 = n > 0 ? head1 : head2;//较长的链表
        cur2 = cur1 == head1 ? head2 : head1;//较短的链表
        n = Math.abs(n);

        //先让较长的链表走n步，让两个链表持平
        while (n != 0) {
            cur1 = cur1.next;
            n--;
        }
        //两个链表同步向下:
        // 如果中途能碰上，则获得第一个交点，否则说明两链表无交点
        while (cur1 != cur2) {
            cur1 = cur1.next;
            cur2 = cur2.next;
        }

        return cur1;
    }

    /**
     * 获取第一个入环节点， 如果不是有环的链表，则返回null
     *
     * @param head
     * @return
     */
    private static Node getLoopNode(Node head) {
        if (head == null || head.next == null || head.next.next == null) {
            return null;
        }
        Node slow = head.next;
        Node fast = head.next.next;

        //快慢指针移动，直到两指针碰上
        while (fast != slow) {
            if (fast.next == null || fast.next.next == null) {
                return null;
            }
            slow = slow.next;
            fast = fast.next.next;
        }

        //接着，快指针来到开头，然后步长变成1，与慢指针同步移动，直到两者再次碰上
        //碰上的节点就是入环节点
        //证明:略
        fast = head;
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return fast;
    }

    public static void main(String[] args) {
        Node head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);

        // 0->9->8->6->7->null
        Node head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(findFirstIntersectNode(head1, head2).value);

        // 1->2->3->4->5->6->7->4...
        head1 = new Node(1);
        head1.next = new Node(2);
        head1.next.next = new Node(3);
        head1.next.next.next = new Node(4);
        head1.next.next.next.next = new Node(5);
        head1.next.next.next.next.next = new Node(6);
        head1.next.next.next.next.next.next = new Node(7);
        head1.next.next.next.next.next.next = head1.next.next.next; // 7->4

        // 0->9->8->2...
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next; // 8->2
        System.out.println(findFirstIntersectNode(head1, head2).value);

        // 0->9->8->6->4->5->6..
        head2 = new Node(0);
        head2.next = new Node(9);
        head2.next.next = new Node(8);
        head2.next.next.next = head1.next.next.next.next.next; // 8->6
        System.out.println(findFirstIntersectNode(head1, head2).value);
    }
}
